package com.zzl.car.util;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.zzl.car.context.ZdpAliPayProperties;
import com.zzl.car.entity.AliPayBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AliPayUtil {

    @Autowired
    private ZdpAliPayProperties zdpAliPayProperties;

    public  String pay(AliPayBean data) throws AlipayApiException {
        String app_id = zdpAliPayProperties.getAPP_ID();
        String charset = zdpAliPayProperties.getCHARSET();
        String gateway_url = zdpAliPayProperties.getGATEWAY_URL();
        String notify_url = zdpAliPayProperties.getNOTIFY_URL();
        String privary_key = zdpAliPayProperties.getPRIVARY_KEY();
        String public_key = zdpAliPayProperties.getPUBLIC_KEY();
        String return_url = zdpAliPayProperties.getRETURN_URL();
        String sign_type = zdpAliPayProperties.getSIGN_TYPE();
        String format = "json";
        //创建服务端，用于生成签名吧
        AlipayClient alipayClient = new DefaultAlipayClient(gateway_url, app_id,
                privary_key, format, charset, public_key, sign_type);
        //设置请求的参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        //设置同步和异步通知的页面路径
        alipayRequest.setNotifyUrl(notify_url);
        alipayRequest.setReturnUrl(return_url);
        //设置参数
        alipayRequest.setBizContent(JSON.toJSONString(data));
        //发送付款请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println(result);
        return result;
    }
}
